$(function () {
    initPageData();
    initDatagrid();
});

// 刷新首页上的数据
function initPageData() {
    var url = $.util.getRequestJson('/json/data4index.baseInfo.json');
    $.util.get(url, {}, function (res) {
        // 删除完成重新加载
        if (res.success) {
            $('#operatorIndexData').form('load',res.data);
        }
    });
}

function initDatagrid() {
    // 待审核活动列表
    var url = $.util.getRequestJson('/json/sysMessage.listByGroup.json?rows=10');
    $('#listTableActivity').datagrid({
        // type=0 即查询 普通公告类型数据
        url: url ,
        idField:'id',
        fit: true,
        queryParams:{
            verifyStatus: 2,
            groupType: 'activity'
        },
        checkOnSelect: false,
        selectOnCheck: false,
        singleSelect: true,
        fitColumns: true,
        columns: [
            [
                {
                    field: 'informTitle',
                    title: '标题',
                },
                {
                    field: 'createPerson',
                    title: '活动创建者',
                    align: 'center',
                    width:12
                },
                {
                    field: 'beginTime',
                    title: '生效时间',
                    align: 'center',
                    width:15
                },
                {
                    field: 'endTime',
                    title: '失效时间',
                    align: 'center',
                    width:12
                },{
                field: 'context',
                width:20,
                title: '操作',
                align: 'center',
                formatter: function (value, row) {
                    var aEl = '<a href=javascript:findDetailActivity("{0}"); class="easyui-tooltip" title="详情">详情</a>' +
                        ' | <a href=javascript:verifyActivity("{0}"); class="easyui-tooltip" title="审核">审核</a>';;
                    //格式化操作按钮
                    return $.string.format(aEl,row.id);
                }
            }
            ]
        ]
    });
    // 待审核商品列表
    var url2 = $.util.getRequestJson('/json/productManage.productVerifyList.json?rows=10');
    $('#listTableProduct').datagrid({
        // type=0 即查询 普通公告类型数据
        url: url2 ,
        idField:'id',
        fit: true,
        queryParams: {
            verifyStatus: 2
        },
        checkOnSelect: false,
        selectOnCheck: false,
        singleSelect: true,
        fitColumns: true,
        columns: [
            [
                {
                    field: 'id',
                    title: '商品编号',
                    align: 'center',
                    width:20
                },
                {
                    field: 'name',
                    title: '发布商户',
                    align: 'center'
                },
                {
                    field: 'title',
                    title: '商品名称',
                },
                {
                    field: 'needScore',
                    title: '兑换要求积分',
                    align: 'center',
                    width:15
                },
                {
                    field: 'dictText',
                    title: '商品分类',
                    align: 'center',
                    width:18
                },{
                field: 'context',
                width:20,
                title: '操作',
                align: 'center',
                formatter: function (value, row) {
                    var aEl = '<a href=javascript:findDetailProduct("{0}"); class="easyui-tooltip" title="详情">详情</a>' +
                        ' | <a href=javascript:verifyProduct("{0}"); class="easyui-tooltip" title="审核">审核</a>';
                    //格式化操作按钮
                    return $.string.format(aEl,row.id);
                }
            }
            ]
        ]
    });
}

// 查看活动详情
function findDetailActivity(id) {
    var title = "待审核活动详情";
    var url = $.util.getRootPath() + '/page/operatorManager/notice/noticeDetail.html?id=' + id;
    //url = id ? url + '?id=2&typeId=' + typeId : url;
    var dialog = $.easyui.showDialog({
        iconCls: "",
        title: title,
        width: 600,
        height: 400,
        maximizable: true,
        href: url,
        topMost: true,
        enableSaveButton:false,
        closeButtonText:'关闭'
    });
}

// 审核活动
function verifyActivity(id) {
    var title = id ? "审核活动" : "新增";
    var url = $.util.getRootPath() + '/page/operatorManager/activityVerify/verifyActivity.html';
    url = id ? url + '?id=' + id : url;
    var dialog = $.easyui.showDialog({
        iconCls: "",
        title: title,
        width: 800,
        height: 500,
        maximizable: true,
        href: url,
        topMost: true,
        saveButtonText: '提交',
        onSave: function () {
            subForm(dialog);
        }
    });
}

// 查看商品详情
function findDetailProduct(id) {
    var title = "待审核商品详情";
    var url = $.util.getRootPath() + '/page/productManage/productDetail.html?id='+id;
    var dialog = $.easyui.showDialog({
        iconCls:"",
        title: title,
        width: 800,
        height: 560,
        maximizable: true,
        href: url,
        topMost: true,
        enableSaveButton:false,
        closeButtonText:'关闭',
    });
}

// 审核商品内容
function verifyProduct(id) {
    var title = "商品审核";
    var url = $.util.getRootPath() + '/page/operatorManager/productVerify/verifyProduct.html';
    url = url + '?id=' + id;
    var dialog = $.easyui.showDialog({
        iconCls:"",
        title: title,
        width: 800,
        height: 600,
        maximizable: true,
        href: url,
        topMost: true,
        saveButtonText: '保存',
        onSave: function () {
            verifyProductSubForm(dialog);
        }
    });
}

// 审核活动操作
function verifyActivitySubForm() {
    var url = $.util.getRootPath() + 'productManage/checkProduct';
    var form = parent.$('#verifyProduct').form('submit', {
        url: url,
        onSubmit: function () {
            return parent.$('#verifyProduct').form('validate');
        },
        success: function (data) {
            if (data.success) {
                $('#listTable').datagrid('reload');
                dialog.dialog('close');
            }
            $.easyui.messager.show(data.message);
        }
    });
}

// 审核商品操作
function verifyProductSubForm(dialog){
    var commtityType = parent.$("#commtityTypeVerify").combobox('getValues');
    var marketPrice = parent.$("#marketPrice").val();
    var needScore = parent.$("#needScore").val();
    var discountPrice = parent.$("#discountPrice").val();
    var discountScore = parent.$("#discountScore").val();
    var title = parent.$("#title").val();
    var id = parent.$("#id").val();
    var attachedFile = parent.$("#attachedFile").val();
    var attachedFileQuality = parent.$("#attachedFileQuality").val();
    var attributePrice = parent.$('#disPrice').attr('style');
    var attributeScore = parent.$('#disScore').attr('style');
    var attributeQuality = parent.$('#commQuality').attr('style');

    if(title.length>100){
        parent.$.messager.alert('商品名称过长');
        return;
    }
    if (commtityType == '请选择商品类别' || commtityType == "" || commtityType == null) {
        parent.$.messager.alert('请选择商品类别');
        return;
    }
    if(commtityType.length > 5){
        parent.$.messager.alert('最多可选5个分类');
        return;
    }
    if(!/^[+-]?(0|([1-9]\d*))(\.\d+)?$/g.test(marketPrice) || marketPrice.length>18){
        parent.$.messager.alert('请正确输入商品价格');
        return;
    }
    if(!/^[0-9]*$/.test(needScore) || needScore.length>9){
        parent.$.messager.alert('请正确输入兑换积分');
        return;
    }
    if(!attributePrice){
        if(discountPrice == "" || discountPrice == null){
            parent.$.messager.alert('请输入优惠价格');
            return;
        }else{
            if(!/^(0|([1-9]\d*))(\.\d+)?$/.test(discountPrice) || discountPrice.length>18){
                parent.$.messager.alert('请正确输入优惠价格');
                return;
            }
        }
    }
    if(!attributeScore){
        if(discountScore == "" || discountScore == null){
            parent.$.messager.alert('请输入优惠兑换积分');
            return;
        }else{
            if(!/^[0-9]*$/.test(discountScore) || discountScore.length>9){
                parent.$.messager.alert('请正确输入优惠兑换积分');
                return;
            }
        }
    }
    if (attachedFile.length <= 0){
        parent.$.messager.alert('请至少上传一张商品图片');
        return;
    }
    if(!attributeQuality){
        if (attachedFileQuality.length <= 0){
            parent.$.messager.alert('请上传精品专区图片');
            return;
        }
    }

    var url = $.util.getRootPath() + '/productManage/checkProduct?dictionaryId='+commtityType;
    var form = parent.$('#verifyProduct').form('submit', {
        url: url,
        onSubmit: function () {
            return parent.$('#verifyProduct').form('validate');
        },
        success: function (data) {
            if (data.success) {
                $('#listTableProduct').datagrid('reload');
                dialog.dialog('close');
            }
            $.easyui.messager.show(data.message);
        }
    });
}

// 保存项目内容
function subForm(dialog) {
    var messageType = parent.$("#messageType").combobox('getValue');
    if (parent.$("#context").ueditor('getPlainTxt') == "") {
        parent.$.messager.alert('请先填写信息的内容');
        return;
    }
    var url = $.util.getRootPath() + '/sysMessage/save?verify=verify';
    parent.$('#publishNotice').form('submit', {
        url: url,
        onSubmit: function (param) {
            return parent.$('#publishNotice').form('validate');
        },
        success: function (data) {
            if (data.success) {
                $('#listTableActivity').datagrid('reload');
                dialog.dialog('close');
                initPageData();
            }
            $.easyui.messager.show(data.message);
        }
    });
}

function queryProduct() {
    var title = '商品审核';
    var url = $.util.getRootPath() + '/page/operatorManager/productVerify/productPublishedList.html';
    if (parent.$.mainpage) {
        parent.$.mainpage.mainTabs.addModule(title, url);
    } else {
        window.open(url, "_blank");
    }
}

function queryActivity() {
    var title = '活动审核';
    var url = $.util.getRootPath() + '/page/operatorManager/activityVerify/activityPublishedList.html';
    if (parent.$.mainpage) {
        parent.$.mainpage.mainTabs.addModule(title, url);
    } else {
        window.open(url, "_blank");
    }
}


